<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>精美切换按钮</title>
    <style>
        /* 按钮外观样式 */
        .switch-container {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 50px;
        }

        .switch-button {
            position: relative;
            width: 120px;
            height: 50px;
            background-color: #ddd;
            border-radius: 30px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        /* 按钮的圆圈 */
        .switch-button .circle {
            position: absolute;
            top: 5px;
            left: 5px;
            width: 40px;
            height: 40px;
            background-color: white;
            border-radius: 50%;
            transition: transform 0.3s ease;
        }

        /* 按钮状态为开时的样式 */
        .switch-button.active {
            background-color: #4CAF50;
            /* 激活时绿色 */
        }

        .switch-button.active .circle {
            transform: translateX(70px);
            /* 圆圈移到右侧 */
        }

        /* 按钮文本 */
        .switch-text {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            font-weight: bold;
            font-size: 16px;
            color: #fff;
            width: 100%;
            text-align: center;
        }

        .switch-button.active .switch-text {
            color: #fff;
        }
    </style>
</head>

<body>

    <div class="switch-container">
        <div class="switch-button" id="switchButton">
            <div class="switch-text">OFF</div>
            <div class="circle"></div>
        </div>
    </div>

    <script>
        // JavaScript 控制按钮切换效果
        const switchButton = document.getElementById('switchButton');
        const switchText = switchButton.querySelector('.switch-text');

        switchButton.addEventListener('click', () => {
            switchButton.classList.toggle('active');
            if (switchButton.classList.contains('active')) {
                switchText.textContent = 'ON';
            } else {
                switchText.textContent = 'OFF';
            }
        });
    </script>

</body>

</html>